package com.atguigu.tingshu.dispatch.job;


import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserJobHandler {

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;


    @XxlJob("userUpdateExpiredVipStatus")
    public void updateExpiredVipStatus() {
        //获取执行任务的初始时间
        long start = System.currentTimeMillis();


        XxlJobLog xxlJobLog = new XxlJobLog();
        //任务id
        xxlJobLog.setId(XxlJobHelper.getJobId());


        try {

            Result result = userInfoFeignClient.updateExpiredVipStatus();
            if (result == null) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError("调用用户服务的远程接口失败");
            }else {
                xxlJobLog.setStatus(1);
            }

        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError("更新Vip状态时发生了位置的服务错误！"+e.getMessage());
            throw new RuntimeException(e);
        } finally {
            long times = System.currentTimeMillis()-start;
            xxlJobLog.setTimes((int)times);
            this.xxlJobLogMapper.insert(xxlJobLog);
        }
    }


}


